import * as THREE from "three";
import { Character } from "../characters/Character";
import { LoadingManager } from "../core/LoadingManager";
import { ISpawnPoint } from "../interface/ISpawnPoint";
import { World } from "./World";
import * as Utils from "../core/FunctionLibrary";

export class CharacterSpawnPoint implements ISpawnPoint {

    private object: THREE.Object3D;

    constructor ( object: THREE.Object3D ) {
        this.object = object;
    }

    spawn ( loadingManager: LoadingManager, world: World ): void {
        loadingManager.loadGLTF( "./models/boxman.glb", gltf => {

            const player = new Character( gltf );

            const position = new THREE.Vector3();
            this.object.getWorldPosition( position );
            player.setPosition( position.x, position.y, position.z );

            const forward = Utils.getForward( player );
            player.setOrientation( forward, true );

            world.add( player );
            player.takeControl()

        } );
    }

}